package algorithm.linklist;

class DoubleNode1 {
    int value;
    DoubleNode1 next;
    DoubleNode1 pre;

    public DoubleNode1(int value) {
        this.value = value;
    }

//    public DoubleNode revert() {
//        DoubleNode head = this;
//        DoubleNode pre = null;
//        DoubleNode next;
//        while (head!= null) {
//            next = head.next;
//            head.next = pre;
//            pre  = head;
//            head = next;
//        }
//        return pre;
//    }

    public static void main(String[] args) {
        DoubleNode1 node = getRandomNode(4, 10);
        printNode(node);
//        printNode(node.revert());
    }

    public static DoubleNode1 getRandomNode(int len, int maxVal) {
        int val = (int) (Math.random() * maxVal);
        System.out.println("val = " + val);
        DoubleNode1 node = new DoubleNode1(val);
        len--;
        DoubleNode1 next = null;
        DoubleNode1 pre;
        while (len > 0) {
            if (node.next == null) {
                node.next = new DoubleNode1((int) (Math.random() * maxVal));
                next = node.next;
                pre = node;
            } else {
                next.next = new DoubleNode1((int) (Math.random() * maxVal));
                pre = next;
                next = next.next;
            }
            next.pre = pre;
            len--;
        }
        return node;
    }

    private static void printNode(DoubleNode1 node) {
        while (node != null) {
            System.out.print(node.value + " -> ");
            node = node.next;
        }
        System.out.println();
    }

}

